//
//  WmcWatermarkSelectClassifView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/2.
//

import Foundation
import UIKit

// MARK: - [WmcWatermarkSelectClassifView]

class WmcWatermarkSelectClassifView: UICollectionView {
    // Lifecycle

    convenience init() {
        let frame = CGRect(x: 0, y: 0, width: Wmc_screenWidth, height: 74)
        self.init(frame: frame, collectionViewLayout: Self.layout)
        delegate = self
        dataSource = self
        self.yp.registerCells([
            YPRegister(WmcWatermarkSelectClassifCell.self),
        ])
        backgroundColor = .clear
        self.showsVerticalScrollIndicator = false
        self.showsHorizontalScrollIndicator = false
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Internal

    static var layout: UICollectionViewFlowLayout {
        let layout = UICollectionViewFlowLayout
            .YP.CreateGridLayout(scrollDirection: .horizontal,
                                 minimumLineSpacing: 0,
                                 minimumInteritemSpacing: 10,
                                 sectionInset: .YP.Create(horizontal: 16, vertical: 8))
        return layout
    }

    /// 水印列表
    var watermarkList: [WmcWatermarkClassifyModel] = []
    /// 选中列表
    var selectList: [Bool] = []
    /// 分类选择变化
    var classifyChangeSelectBlock: YPBlock.TwoParam<Int, WmcWatermarkClassifyModel>?

    /// 设置水印列表
    func setWatermarkList(_ list: [WmcWatermarkClassifyModel]) {
        watermarkList = list
//        selectList.removeAll()
//        watermarkList.forEach { _ in
//            selectList.append(selectList.count == 0)
//        }
        reloadData()
    }

    /// 选中一个
    func select(at index: Int) {
        selectList.removeAll()
        watermarkList.forEach { _ in
            selectList.append(false)
        }
        guard !selectList.isEmpty else { return }
        selectList[index] = true
        yp.delayLast(0.3) { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.reloadData()
            weakSelf.scrollToItem(at: .init(row: index, section: 0), at: .centeredHorizontally, animated: true)
        }
    }
}

// MARK: - UICollectionViewDelegate

extension WmcWatermarkSelectClassifView: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let model = watermarkList.yp.object(at: indexPath.row) {
            select(at: indexPath.row)
            YPDevice.PlayShake(.light)
            classifyChangeSelectBlock?(indexPath.row, model)
        }
    }
}

// MARK: - UICollectionViewDelegateFlowLayout

extension WmcWatermarkSelectClassifView: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        guard let model = watermarkList.yp.object(at: indexPath.row) else { return .zero }
        let herSp: CGFloat = 20
        let nameLength = (model.name.count * 14).yp.cgFloat
        let height = collectionView.frame.height - Self.layout.sectionInset.yp.vertical
        return CGSize(width: herSp + nameLength,
                      height: height)
    }
}

// MARK: - UICollectionViewDataSource

extension WmcWatermarkSelectClassifView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return watermarkList.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: WmcWatermarkSelectClassifCell = collectionView.yp.dequeueReusableCell(for: indexPath)
        if let model = watermarkList.yp.object(at: indexPath.row) {
            let isSelect = selectList.yp.object(at: indexPath.row) ?? false
            if indexPath.row == 8{
                print("")
            }
            cell.configModel(model, isSelect: isSelect)
            Wmc_SpringAnimation() {
//                if isSelect {
//                    cell.transform = CGAffineTransform.YP.CreateScale(1.1)
//                } else {
//                    cell.transform = CGAffineTransform.YP.CreateScale(1)
//                }
            }
        }
        return cell
    }
}
